# -*- coding: utf-8 -*-
# author:jiangsibei
# desc: 该文件主要为设置相关信息的方法:比如：设置已学字数、设置已学主题数
from tools import  zyb_login
import os,inspect
import logging
import time
from tools import Config
import requests
import json
rootPath = os.path.abspath(os.path.dirname(__file__) + os.path.sep + ".")
configPath = rootPath + "/config.ini"

# 获取当前文件的上层路径
parentPath = os.path.abspath(os.path.dirname(inspect.getfile(inspect.currentframe())) + os.path.sep + ".")
# 获取当前项目的根路径
rootPath = os.path.abspath(os.path.dirname(parentPath) + os.path.sep + ".")
configPath = rootPath + "/config.ini"


#ips登陆
def ipslogin(username, password, url, sid):
    if os.environ.get("cookie_" + username):
        logging.debug('用户{}已登录，cookie为{}'.format(username, os.environ["cookie_" + username]))
    else:
        service = url
        arrInput = {
            'username': username,
            'password': password,
            'sid': sid,
            "service": service
        }
        logging.info('{} {} {} {}'.format(username,password,sid,service))
        cookie = zyb_login.ips_Login(arrInput)
        if(len(cookie) == 0):  #若获取的cookies为空，则等待2s重试
            time.sleep(2)
            cookie = zyb_login.ips_Login(arrInput)
        cookie_user = ""
        for k, v in cookie.items():
            cookie_user = cookie_user + "ZYBIPSCAS" + "=" + v + ";"  #为了解决目前获取到的cookies是ZYBIPS="xxxxxxxx"
            #cookie_user = cookie_user + k + "=" + v + ";"
        os.environ["cookie_" + username] = cookie_user
        logging.info('用户{}登录成功，cookie为{}'.format(username, os.environ["cookie_" + username]))
    return os.environ["cookie_" + username]
#获取cookies
def get_cookies(service,email,passwd,sid):
    cookie = ipslogin(email, passwd,service, sid)
    print(cookie)
    return cookie


#修改学习进度
def fix_progress(phone,num,type):
    """
    :param phone: 手机号
    :param num: 修改的数量
    :param type: L1：1  L2：2
    :return:
    """
    #1、ips登陆
    #2、修改:

    username = Config.getProConfig(configPath,"ips_username","basic")[0]
    password = Config.getProConfig(configPath,"ips_password","basic")[0]
    service = Config.getProConfig(configPath,"cms_url","basic")[0]
    sid = Config.getProConfig(configPath,"cms_sid","basic")[0]
    Cookie = get_cookies(service,username,password,sid)


    url = "https://shizi-cms.zuoyebang.cc/shizi-cms/learn-progress/qa/"+ str(phone)

    try:
        data = {
            'appId': 0,
            'gradeOrder': num,
            'groupType': type,
        }
    except:
        return {}
    cms_cookies = {
        "cookie": Cookie
    }
    cms_header = {
        "referer":"xxxxxxxx",
        "content-type":"application/json",
        "cookie": Cookie
    }
    http_request = requests.session()
    http_response = http_request.post(url, json=data,headers=cms_header,cookies=cms_cookies)
    try:
        resp = json.loads(http_response.text)
        respose = resp["errStr"]
        logging.debug(f"进度修改结果{respose}")
        print(respose)
        return respose

    except:
        return False



if __name__ == "__main__":
    fix_progress(11692002223,0,1)